﻿/**********************************************
 * 功能名称：燃动系数管理
编写人：  杨莹
建立时间： 杨 2009年8月25日
说明：
管理各作业中心的水、电、空压、冷冻、蒸汽等系数。
 **************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.CostDriver
{
    public partial class PowerIndex : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        
        public string alert;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadioButtonList_PowerSelect.SelectedIndex = 0;
                Panel_MainQuery.Visible = false;
            
            
            }
        }


        private void BindMyData(int PageIndex)
        {
           

            int skipAccount = PageIndex * 10;

            string MyQueryCenterName = TextBox_CenterName.Text;

            string MyCostItem = "";
            switch(RadioButtonList_PowerSelect.SelectedIndex)
            {
                case 0 :
                    {
                        MyCostItem = "SF";

                    break;
                    }                     
                case 1 :
                    {
                        MyCostItem = "DF";
                    break;
                    }
                 case 2 :
                    {
                        MyCostItem = "KY";
                    break;
                    }
                 case 3 :
                    {
                         MyCostItem = "LD";
                    break;
                    }
                  case 4 :
                    {
                           MyCostItem = "ZQ";

                    break;
                    }
            }

            CurrentCostItem.Value = MyCostItem;
            var MyQuery = (from a in db.TABC_JS_CostCenter
                           join b in
                               (from c in db.TABC_DM_PowerIndex where c.成本细项ID == MyCostItem select c)
                           on a.作业中心ID equals b.作业中心ID
                           into temps                      
                           from temp in temps.DefaultIfEmpty()
                             where a.停用 == false && new string[]{"BF","BZ","BH"}.Contains(a.类型)
                             && a.作业中心名称.Contains(MyQueryCenterName)
                                orderby  a.作业中心名称
                           select new                          
                           {
                             a.作业中心ID,
                             a.作业中心名称,
                             系数 = (temp.系数 == null ? 0 :temp.系数)
                           
                           }).Skip(skipAccount).Take(10);
               

                    GridView_Query.DataSource = MyQuery;
                    GridView_Query.DataBind();
                    if (MyQuery.Count() == 0)
                    { Panel_MainQuery.Visible = false; }
                    else
                    { Panel_MainQuery.Visible = true; };
            


        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            string MyQueryCenterName = TextBox_CenterName.Text;

            string MyCostItem = "";
            switch (RadioButtonList_PowerSelect.SelectedIndex)
            {
                case 0:
                    {
                        MyCostItem = "SF";

                        break;
                    }
                case 1:
                    {
                        MyCostItem = "DF";
                        break;
                    }
                case 2:
                    {
                        MyCostItem = "KY";
                        break;
                    }
                case 3:
                    {
                        MyCostItem = "LD";
                        break;
                    }
                case 4:
                    {
                        MyCostItem = "ZQ";

                        break;
                    }
            }


             MyQuery = (from a in db.TABC_JS_CostCenter
                           join b in (from c in db.TABC_DM_PowerIndex  where c.成本细项ID == MyCostItem  select c)
                           on a.作业中心ID equals b.作业中心ID
                           into temps
                           from temp in temps.DefaultIfEmpty()
                           where a.停用 == false && new string[] { "BF", "BZ", "BH" }.Contains(a.类型)
                            && a.作业中心名称.Contains(MyQueryCenterName)
                           orderby a.作业中心名称
                           select new
                           {
                               a.作业中心ID,
                               a.作业中心名称,
                               系数 = (temp.系数 == null ? 0 : temp.系数)

                           }).Count();
               

            return MyQuery;
        }

        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 20) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }


        //修改当前页的数据。
        protected void Button_Save_Click(object sender, EventArgs e)
        {

            try
            {
                for (int i = 0; i < GridView_Query.Rows.Count; i++)
                {
                    GridViewRow myrows = GridView_Query.Rows[i];
                    string MyCenterId = myrows.Cells[0].Text;
                    string MyCostItem = CurrentCostItem.Value.ToString();
                    string MyIndex = ((TextBox)myrows.Cells[2].FindControl("TextBox_Value")).Text;
                    var EditRows = from a in db.TABC_DM_PowerIndex
                                   where a.作业中心ID == Convert.ToInt32(MyCenterId)
                                   && a.成本细项ID == MyCostItem
                                   select a;
                    if (EditRows.Count() == 1)
                    {
                        var EditRow = EditRows.Single();

                        EditRow.系数 = Convert.ToDouble(MyIndex);

                        db.SubmitChanges();
                    }
                    else
                    {
                        db.TABC_DM_PowerIndex.DeleteAllOnSubmit(EditRows);
                        db.SubmitChanges();

                        TABC_DM_PowerIndex AddRow = new TABC_DM_PowerIndex();

                        AddRow.公司ID = "hisun";
                        AddRow.账套ID = "001";
                        AddRow.作业中心ID = Convert.ToInt32(MyCenterId);
                        AddRow.成本细项ID = MyCostItem;
                        AddRow.系数 = Convert.ToDouble(MyIndex);

                        db.TABC_DM_PowerIndex.InsertOnSubmit(AddRow);

                        db.SubmitChanges();
                    }




                }

                BindMyData(Convert.ToInt32(CurrentPage.Value));

                alert = "<script>alert('保存成功');</script>";
            }
            catch 
            { alert = "<script>alert('保存失败');</script>";
                alert = "<script>alert('保存失败');</script>";
            }
           
        }

        protected void RadioButtonList_PowerSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel_MainQuery.Visible = false;
        }
    }
}
